﻿using Digitalmes.Events;
using Digitalmes.Model;
using Digitalmes.Services;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Digitalmes.Models
{

    public class AlarmTag : ISysTag
    {
        public string OpCode { get; set; }
        public long ID { get; }
        public string Name { get; }
        public string Address { get; }
        public long? EquipmentId { get; }
        public string Description { get; }
        public TagDefineEnum TagType { get; }
        public TagDataTypeEnum DataType { get; }
        public int? ScanRate { get; }
        public TagGroupEnum GroupType { get; }
        public TagAccessModeEnum AccessMode { get; }
        public DriverTypeEnum? DriverType { get; }
        public decimal? UpLimit { get; }
        public decimal? LowerLimit { get; }
        public bool ActiveLimit { get; }
        public string EquipCode { get; }
        public bool IsValid { get; }

        public TypeCode ValueType { get; }

        public List<ISysTag> TagGroup { get; set; }
        /// <summary>
        /// 构造函数，初始化只读标签
        /// </summary>
        public AlarmTag(
            long id,
            string tagName,
            string address,
            long? equipmentId,
            string description,
            TagDefineEnum tagType,
            TagDataTypeEnum dataType,
            int? scanRate,
            TagGroupEnum groupType,
            TagAccessModeEnum accessMode,
            DriverTypeEnum? driverType,
            decimal? upLimit,
            decimal? lowerLimit,
            bool activeLimit,
            string equipCode,
             TypeCode type,
           string groupName )
        {
            ID = id;
            Name = tagName;
            Address = address;
            EquipmentId = equipmentId;
            Description = description;
            TagType = tagType;
            DataType = dataType;
            ScanRate = scanRate;
            GroupType = groupType;
            AccessMode = accessMode;
            DriverType = driverType;
            UpLimit = upLimit;
            LowerLimit = lowerLimit;
            ActiveLimit = activeLimit;
            EquipCode = equipCode; 
            this.ValueType = type;
            this.DriverGroup = groupName;
        }

        private object _value;

        public DateTime LastUpdateTime { get; private set; }

        //    // 公开事件（支持 +=/-= 动态管理）
        public event EventHandler<TagValueChangedEventArgs> ValueChanged;
        public object Value
        {
            get => _value;
            set
            {
                if (Equals(_value, value)) return;

                var oldValue = _value;
                _value = value;
                LastUpdateTime = DateTime.Now;
                this.ValueChanged?.Invoke(this, new TagValueChangedEventArgs(oldValue, value));
            }
        }

        public DataQuality Quality { get; set; }


        public IDriver Driver { get; set; }

      

       

        public bool IsReadOnly { get ; set ; }
        public VariableScope Scope { get ; set; }


        public string DriverGroup { get ; set ; }

        public void SetValue(object value)
        {
            this.Value = value;
        }

    }

    
}
